package com.douqu.game.battle.controller.sprite;

import com.douqu.game.core.e.E_PlayerStatus;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.netty.NettyConnection;
import com.douqu.game.core.protobuf.SGMainProto;
import com.douqu.game.core.util.LogUtils;

/**
 * Created by bean on 2017/7/18.
 */
public class PlayerController extends SpriteController {


    private NettyConnection tcpConnection;

    private Player player;

    private boolean isAutoBattle = false;

    /**
     * 上次发送消息给客户端的时间
     */
    private long lastSendMsgTime;


    public PlayerController(Player player, int serverId)
    {
        this.player = player;
        this.lastSendMsgTime = DataFactory.currentTime;

        setStatus(E_PlayerStatus.FREE);
        setServerId(serverId);
    }

    public PlayerController(Player player, NettyConnection connection, int serverId)
    {
        this(player, serverId);

        this.tcpConnection = connection;
        if(tcpConnection != null)
            tcpConnection.setObject(this);
    }

    @Override
    public boolean isOnlinePlayer()
    {
        return tcpConnection != null;
    }



    public void update(long currentMillis)
    {

    }

    /**
     * 发送消息
     * @param msgID
     * @param data
     */
    public void sendMsg(int msgID, byte[] data)
    {
        if(tcpConnection != null)
        {
            tcpConnection.sendMsg(msgID, data);

            lastSendMsgTime = DataFactory.currentTime;

            LogUtils.debug(player.getName() + " 发送消息给客户端：code:" + SGMainProto.E_MSG_ID.forNumber(msgID) + " " + tcpConnection.getRemoteAddress() + " 　data:" + (data==null?0:data.length));
        }
    }


    public void sendWarn(String key, Object... params)
    {
        if(tcpConnection == null)
            return;

        tcpConnection.sendWarn(key, params);
    }

    public void sendGM(String content)
    {
        if(tcpConnection == null)
            return;

        tcpConnection.sendGM(content);
    }

    public void sendOffline()
    {
        if(tcpConnection == null)
            return;

        LogUtils.warn("发送离线--->" + player.getName());

        tcpConnection.sendOffline();
    }

    public void sendUnknown()
    {
        if(tcpConnection != null)

            tcpConnection.sendUnknown();
    }


    public void destroy()
    {
        if(tcpConnection != null)
        {
            LogUtils.info(player.getName() + " destroy!");

            tcpConnection.destroy();

            tcpConnection = null;
        }
    }




    @Override
    public String getObjectIndex()
    {
        return player.getObjectIndex();
    }

    @Override
    public String getName()
    {
        return player.getName();
    }

//    public void sendAlert(SGCommonProto.E_NOTIFY_TYPE notifyType)
//    {
//        if(tcpConnection != null)
//        {
//            SGSystemProto.S2C_NotifyAlert.Builder response = SGSystemProto.S2C_NotifyAlert.newBuilder();
//            response.setType(notifyType);
//            tcpConnection.sendMsg(SGMainProto.E_MSG_ID.MsgID_System_NotifyAlert_VALUE, response.build().toByteArray());
//        }
//    }








    public Player getPlayer() {
        return player;
    }



    public NettyConnection getTcpConnection() {
        return tcpConnection;
    }

    public void setTcpConnection(NettyConnection tcpConnection) {
        this.tcpConnection = tcpConnection;
    }

    public boolean isAutoBattle() {
        return isAutoBattle;
    }

    public void setAutoBattle(boolean isAutoBattle) {
        this.isAutoBattle = isAutoBattle;
    }

    public long getLastSendMsgTime() {
        return lastSendMsgTime;
    }

    @Override
    public String toString() {
        return "{" +
                "source=" + player +
                ", tcpConnection=" + (tcpConnection!=null?tcpConnection.getChannel().remoteAddress():null) +
                ", status=" + status +
                "} " + super.toString();
    }
}
